/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.images;

import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;

/**
 *
 * @author Rik Bauwens
 */
public class ScaledImageIcon
{
        private ImageIcon imageIcon;

        public ScaledImageIcon(String path, String description)
        {
                URL imgURL = getClass().getResource(path);
                if (imgURL != null) {
                    imageIcon = new ImageIcon(imgURL, description);
                }
        }

        public ScaledImageIcon setMaximumWidth(int maxWidth)
        {
                return setMaximumDimension(maxWidth, imageIcon.getIconHeight());
        }

        public ScaledImageIcon setMaximumHeight(int maxHeight)
        {
                return setMaximumDimension(imageIcon.getIconWidth(), maxHeight);
        }

        public ScaledImageIcon setMaximumSide(int maxSide)
        {
                return setMaximumDimension(maxSide, maxSide);
        }

        public ScaledImageIcon setMaximumDimension(int maxWidth, int maxHeight)
        {
                int width = maxWidth;
                int height = (int) (imageIcon.getIconHeight() * (1.0 * maxWidth / imageIcon.getIconWidth()));
                if(height > maxHeight)
                {
                        height = maxHeight;
                        width = (int) (imageIcon.getIconWidth() * (1.0 * maxHeight / imageIcon.getIconHeight()));
                }
                scale(width, height);
                return this;
        }

        public ScaledImageIcon setScale(int percent)
        {
                scale(percent);
                return this;
        }

        private void scale(int width, int height)
        {       
                imageIcon = new ImageIcon(imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
        }

        private void scale(int percent)
        {
                int width = (int) (imageIcon.getIconWidth() * (percent / 100.0));
                int height = (int) (imageIcon.getIconHeight() * (percent / 100.0));
                imageIcon = new ImageIcon(imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
        }

        public ImageIcon getImageIcon()
        {
                return imageIcon;
        }

        public Image getImage()
        {
                return imageIcon.getImage();
        }
}
